N = int(input())
m = 0
if 999 < N < 10000:
    N1 = N // 1000
    N2 = N // 100 % 10
    N3 = N // 10 % 10
    N4 = N % 1000 % 10
    N12 = N // 100
    N23 = N % 1000 // 10
    N34 = N % 100 % 100
    N123 = N // 10
    N234 = N % 1000
    a = [N1, N2, N3, N4, N12, N23, N34, N123, N234]
    for i in a:
        for j in range(2, i):
            if i % j == 0:
                break
        else:
            m = max(m, i)
print(m)